//
//  UgcDetailCummentHeadCell.m
//  ZITHER
//
//  Created by weixin on 2018/9/11.
//  Copyright © 2018年 weixn. All rights reserved.
//

#import "UgcDetailCummentHeadCell.h"
#import "Ugc_Detail_VC_WX.h"
#import "Ugc_HomePage_VC_V3.h"
#import "MainFirst_Magazine_VC_WX.h"

#define kGAP 10
#define kAvatar_Size 40

@implementation UgcDetailCummentHeadCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    self.backgroundColor = [UIColor colorWithHex:@"f5f5f5"];
  
    [self setUI];
    [self setMasonry];
}

-(void)setModelComment:(UgcCommentListModelSub *)modelComment{
    _modelComment = modelComment;
    

    self.nameUser.text = modelComment.nickName;
    [self.picUser sd_setImage:modelComment.avatar];
    self.comment.text = modelComment.commentContent;

    
    
    
//    self.comment.text = [NSString stringWithFormat:@"%@:%@",modelComment.nickName,modelComment.commentContent];
//    if ([modelComment.userId isEqualToString:kUSER_INFO_MANAGER.userId]) {
//        [self.userDelete mas_updateConstraints:^(MASConstraintMaker *make) {
//            //            make.bottom.equalTo(self).offset(-20);
//            make.height.mas_equalTo(20);
//        }];
//        self.userDelete.hidden = NO;
//    }else{
//        self.userDelete.hidden = YES;
//        [self.userDelete mas_updateConstraints:^(MASConstraintMaker *make) {
//            make.height.mas_equalTo(0);
//            //            make.bottom.equalTo(self).offset(-12);
//        }];
//    }
    [self setNeedsLayout];
//    [self configComment];
}

-(void)setMasonry{
    
    [self.comment mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.nameUser.mas_bottom).offset(0);
        make.left.equalTo(self.picUser.mas_right).offset(10);
        make.right.equalTo(self).offset(-14);

        make.bottom.equalTo(self.mas_bottom).offset(-15);
    }];
    
    [self.nameUser mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.picUser.mas_centerY).offset(0);
        make.left.equalTo(self.picUser.mas_right).offset(10);
    }];
    
    [self.picUser mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self).offset(20);
        make.left.equalTo(self).offset(14);
        make.height.width.mas_equalTo(30);
//        make.bottom.equalTo(self).offset(-8);
    }];
    
    [self.viewLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mas_top).offset(0);
        make.left.equalTo(self.picUser.mas_right).offset(10);
        make.right.equalTo(self).offset(-15);
        make.height.mas_equalTo(1);
    }];
    
//    [self.userDelete mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.equalTo(self.comment.mas_bottom).offset(5);
//        make.left.equalTo(self).offset(14);
//        make.bottom.equalTo(self).offset(-20);
//    }];
    
}

-(void)setUI{
    [self addSubview:self.comment];
    [self addSubview:self.picUser];
    [self addSubview:self.nameUser];
    [self addSubview:self.viewLine];

//    [self addSubview:self.userDelete];
}

-(QMUILabel *)comment{
    if (!_comment) {
        _comment = [[QMUILabel alloc]initWithFrame:CGRectMake(0, 0, 1, 1)];
        _comment.numberOfLines = 0;
        _comment.font = [UIFont fontWithName:@"PingFang-SC-Regular" size:14];
        _comment.textColor = [UIColor colorWithRed:51/255.0 green:51/255.0 blue:51/255.0 alpha:1/1.0];
//        _comment.text = @"一堆没用的回复一堆没用的回复一堆没用的回复一堆没用的回复一堆没用的回复一堆没用的回复一堆没用的回复一堆没用的回复一堆没用的回复";
    }
    return _comment;
}


-(UIView *)viewLine{
    if (!_viewLine) {
        _viewLine = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];
        _viewLine.backgroundColor = [UIColor colorWithHex:@"eeeeee"];
    }
    return _viewLine;
}

-(BaseImageV *)picUser{
    if (!_picUser) {
        _picUser = [[BaseImageV alloc]initializeFrame:CGRectMake(0, 0, 0, 0) contentMode:2];
        @weakify(self);
        [_picUser setRound:15];
        [[_picUser addTag]subscribeNext:^(id  _Nullable x) {
            @strongify(self);
            Ugc_HomePage_VC_V3 *vc = [[Ugc_HomePage_VC_V3 alloc]init];
            vc.userName = self.modelComment.nickName;
            vc.userId = self.modelComment.userId;
            vc.userAvatar = self.modelComment.avatar;
            [self.getNav pushViewController:vc animated:YES];
        }];
    }
    return _picUser;
}

-(BaseLab *)nameUser{
    
    if (!_nameUser) {
        _nameUser = [[BaseLab alloc]initializeFrame:CGRectMake(0, 0, 0, 0) text:@"" font:12 textColor:[UIColor colorWithHex:@"333333"] isBold:NO numberOfLines:1];

        _nameUser.font = [UIFont fontWithName:@"PingFangSC-Semibold" size:14];
        _nameUser.textColor = [UIColor colorWithRed:51/255.0 green:51/255.0 blue:51/255.0 alpha:1/1.0];
        @weakify(self);
        [[_nameUser addTag]subscribeNext:^(id  _Nullable x) {
            @strongify(self);
            Ugc_HomePage_VC_V3 *vc = [[Ugc_HomePage_VC_V3 alloc]init];
            vc.userName = self.modelComment.nickName;
            vc.userId = self.modelComment.userId;
            vc.userAvatar = self.modelComment.avatar;
            [self.getNav pushViewController:vc animated:YES];
        }];
    }
    return _nameUser;
}


-(QMUIButton *)userDelete{
    if (!_userDelete) {
        _userDelete = [[QMUIButton alloc]initWithFrame:CGRectMake(0, 0, 1, 1)];
        [_userDelete setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        _userDelete.titleLabel.font = [UIFont systemFontOfSize:12];
        [_userDelete setTitle:@"删除" forState:UIControlStateNormal];
        _userDelete.hidden = YES;
        @weakify(self);
        [[_userDelete rac_signalForControlEvents:(UIControlEventTouchUpInside)] subscribeNext:^(__kindof UIControl * _Nullable x) {
            @strongify(self);
            [[serviceDefualt loadDelUserCommentCommentId:self.modelComment.commentId reponseObjctClass:nil]subscribeNext:^(ResponseData * x) {
                if (x.success) {
                  
                    if (self.index == 0) {
                        Ugc_Detail_VC_WX *vc = (Ugc_Detail_VC_WX*)[self getViewController:[Ugc_Detail_VC_WX class]];
//                        if (vc.blockDel) {
//                            vc.blockDel();
//                        }
                        
                        [vc.tbv.mj_header beginRefreshing];
                        
                    }else{
                        MainFirst_Magazine_VC_WX *vc = (MainFirst_Magazine_VC_WX*)[self getViewController:[MainFirst_Magazine_VC_WX class]];
//                        if (vc.blockDel) {
//                            vc.blockDel();
//                        }
                        [vc.Tbv Starefreshing];

                    }

                    [Tools showTips:@"删除成功"];
                }else{
                     [Tools showTips:x.errorInfo];
                }
            }];
    }];
    }
    return _userDelete;
}


-(void)configComment{
    
    NSString *str  = nil;
    str= [NSString stringWithFormat:@"%@  %@",
          self.modelComment.nickName,self.modelComment.commentContent];
    
    NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:str];
    NSMutableParagraphStyle *muStyle = [[NSMutableParagraphStyle alloc] init];
    
    if ([text.string isMoreThanOneLineWithSize:CGSizeMake(kScreenW-kGAP-kAvatar_Size-2*kGAP, CGFLOAT_MAX) font:[UIFont fontWithName:@"PingFang-SC-Regular" size:13] lineSpaceing:5.0]) {//margin
        muStyle.lineSpacing = 5.0;//设置行间距离
    }else{
        muStyle.lineSpacing = CGFLOAT_MIN;//设置行间距离
    }
    
    [text addAttribute:NSParagraphStyleAttributeName value:muStyle range:NSMakeRange(0, text.length)];
    
    [text addAttribute:NSForegroundColorAttributeName
                 value:[UIColor blackColor]
                 range:NSMakeRange(0, self.modelComment.nickName.length)];
    [text addAttribute:NSFontAttributeName
                 value:[UIFont fontWithName:@"PingFang-SC-Regular" size:13]
                 range:NSMakeRange(0, self.modelComment.nickName.length)];
    
    
    self.comment.attributedText = text;
    @weakify(self);
    [self.comment yb_addAttributeTapActionWithStrings:@[self.modelComment.nickName] tapClicked:^(NSString *string, NSRange range, NSInteger index) {
        @strongify(self);
        Ugc_HomePage_VC_V3 *vc = [[Ugc_HomePage_VC_V3 alloc]init];
        vc.userName = self.modelComment.nickName;
        vc.userId = self.modelComment.userId;
        vc.userAvatar = self.modelComment.avatar;
        [self.getNav pushViewController:vc animated:YES];
    }];
    //设置是否有点击效果，默认是YES
    self.comment.enabledTapEffect = NO;
    
    
}


@end
